package com.hosa.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hosa.constants.SystemConstants;
import com.hosa.domain.ResponseResult;
import com.hosa.domain.entity.Article;
import com.hosa.domain.entity.Category;
import com.hosa.domain.vo.ArticleDetailVo;
import com.hosa.domain.vo.ArticleListVo;
import com.hosa.domain.vo.HotArticleVo;
import com.hosa.domain.vo.PageVo;
import com.hosa.mapper.ArticleMapper;
import com.hosa.service.ArticleService;
import com.hosa.service.CategoryService;
import com.hosa.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper,Article> implements ArticleService {
    @Autowired
    private CategoryService categoryService;
    @Override
    public ResponseResult hotArticleList() {
        LambdaQueryWrapper<Article> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        queryWrapper.orderByDesc(Article::getViewCount);
        Page<Article> page=new Page<Article>(1,10);
        page(page,queryWrapper);
        List<Article> articles = page.getRecords();
        BeanCopyUtils.copyBeanList(articles, HotArticleVo.class);
        return  ResponseResult.okResult(articles);
    }
    @Override
    public ResponseResult articleList(Integer pageNum, Integer pageSize, Long
            categoryId) {
//查询条件
        LambdaQueryWrapper<Article> lambdaQueryWrapper = new
                LambdaQueryWrapper<>();
// 如果 有categoryId 就要 查询时要和传入的相同
        lambdaQueryWrapper.eq(Objects.nonNull(categoryId)&&categoryId>0
                ,Article::getCategoryId,categoryId);
// 状态是正式发布的
        lambdaQueryWrapper.eq(Article::getStatus,SystemConstants.ARTICLE_STATUS_NORMAL)
        ;
// 对isTop进行降序
        lambdaQueryWrapper.orderByDesc(Article::getIsTop);
//分页查询
        Page<Article> page = new Page<>(pageNum,pageSize);
        page(page,lambdaQueryWrapper);
        List<Article> articles = page.getRecords();
//查询categoryName
        articles.stream()
                .map(article->article.setCategoryName(categoryService.getById(article.getCategoryId()).getName()))
                .collect(Collectors.toList());
//articleId去查询articleName进行设置
// for (Article article : articles) {
// Category category =
//        categoryService.getById(article.getCategoryId());
// article.setCategoryName(category.getName());
// }
//封装查询结果
        List<ArticleListVo> articleListVos = BeanCopyUtils.copyBeanList(page.getRecords(), ArticleListVo.class);
        PageVo pageVo = new PageVo(articleListVos,page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult getArticleDetail(Long id) {
        //根据id查询文章
        Article article=getById(id);
        //转换为vo
        ArticleDetailVo articleDetailVo = BeanCopyUtils.copyBean(article, ArticleDetailVo.class);
        //根据分类id查分类名
        Long categroyId= articleDetailVo.getCategoryId();
        Category category = categoryService.getById(categroyId);
        if (category != null) {
            articleDetailVo.setCategoryName(category.getName());
        }
        return ResponseResult.okResult();
    }
}
